package com.libertyville.domain.orgmng;

import com.libertyville.domain.orgmng.validator.CommonValidator;
import com.libertyville.domain.orgmng.validator.OrgTypeValidator;

import java.time.LocalDateTime;

public class OrgBuilder {
    private Integer tenantId;
    private Integer superiorId;
    private Integer leaderId;
    private String name;
    private String orgTypeCode;
    private String createdBy;
    private CommonValidator commonValidator;
    private OrgTypeValidator orgTypeValidator;

    public OrgBuilder(CommonValidator commonValidator, OrgTypeValidator orgTypeValidator) {
        this.commonValidator = commonValidator;
        this.orgTypeValidator = orgTypeValidator;
    }

    public OrgBuilder tenantId(Integer tenantId) {
        this.tenantId = tenantId;
        return this;
    }

    public OrgBuilder superiorId(Integer superiorId) {
        this.superiorId = superiorId;
        return this;
    }

    public OrgBuilder leaderId(Integer leaderId) {
        this.leaderId = leaderId;
        return this;
    }

    public OrgBuilder name(String name) {
        this.name = name;
        return this;
    }

    public OrgBuilder orgTypeCode(String orgTypeCode) {
        this.orgTypeCode = orgTypeCode;
        return this;
    }

    public OrgBuilder createdBy(String createdBy) {
        this.createdBy = createdBy;
        return this;
    }

    public Org buildOrg() {
        validate();

        Org org = new Org(this.tenantId, this.orgTypeCode, LocalDateTime.now(), this.createdBy);
        org.setSuperiorId(this.superiorId);
        org.setLeaderId(this.leaderId);
        org.setName(this.name);
        org.setOrgStatus(OrgStatus.EFFECTIVE);
        org.setLastUpdatedAt(LocalDateTime.now());
        org.setLastUpdatedBy(this.createdBy);
        return org;
    }

    private void validate() {
        commonValidator.validateTenant(tenantId);
        orgTypeValidator.validateOrgType(tenantId, orgTypeCode);
    }
}
